package com.kldy8.kldyw;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.kldy8.kldyw.find.CustomWebView;
import com.kldy8.kldyw.find.NewInfoActivity;
import com.kldy8.kldyw.system.UrlBuilder;
import com.kldy8.kldyw.web.AppWebUrl;
import com.kldy8.kldyw.find.project.ProjectActivity;
import com.kldy8.kldyw.release.fragment.BaseFragment;
import com.kldy8.kldyw.find.script.ScriptActivity;

/**
 * Created by Qun Chen on 2016/11/1 0001.
 * 发现模块fragment，采用webView加载界面。
 * <p>
 * 通过拦截链接，进行界面的跳转
 */
public class FindFragment extends BaseFragment {
    private View mRootView;
    private CustomWebView webView;

    private RelativeLayout reloadlayout;
    private RelativeLayout loadinglayout;
    private ImageView reload_img;
    private String urlString;


    private static final String URL_LIVE_INDEX = UrlBuilder.URL_BASE_API+"/live/live_index";//直播的url
    private static final String URL_SCRIPT = UrlBuilder.URL_BASE_API+"/script/script_list";//剧本的url
    private static final String URL_COLUMN = UrlBuilder.URL_BASE_API+"/news/column";//大咖
    private static final String URL_CHIPS = UrlBuilder.URL_BASE_API+"/chips/chips_list";//项目
    private static final String URL_NEARBY = UrlBuilder.URL_BASE_API+"/detection/lists";//附近

    public static FindFragment getInstance() {
        FindFragment fragment = new FindFragment();
        return fragment;
    }

    public static FindFragment newInstance(String param1)
    {
        FindFragment fragment = new FindFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == mRootView) {
            mRootView = inflater.inflate(R.layout.fragment_find_i, container, false);
            urlString = AppWebUrl.createUrl(AppWebUrl.URL_FIND);
            findView();
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null)
            parent.removeView(mRootView);

        return mRootView;
    }

    private void findView() {

        Toolbar toolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        toolbar.setTitle(getResources().getString(R.string.find));//设置主标题

        loadinglayout = (RelativeLayout) mRootView.findViewById(R.id.loadinglayout);
        reloadlayout = (RelativeLayout) mRootView.findViewById(R.id.reloadlayout);
        reload_img = (ImageView) mRootView.findViewById(R.id.reload_img);
        reload_img.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                loadinglayout.setVisibility(View.VISIBLE);
                webView.loadUrl(urlString);
            }
        });


        webView = (CustomWebView) mRootView.findViewById(R.id.webView_find);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl)
            {
                // TODO Auto-generated method stub
//                super.onReceivedError(view, errorCode, description, failingUrl);
                loadinglayout.setVisibility(View.GONE);
                reloadlayout.setVisibility(View.VISIBLE);
            }
            @Override
            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
                loadinglayout.setVisibility(View.GONE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                reloadlayout.setVisibility(View.GONE);
                Log.e("----url-------",url);
//                if (url.equals(URL_LIVE_INDEX)) {
//                    ////  2016/11/1 0001 直播
//                    startActivityRightToLeft(new Intent(getActivity(), FindLiveActivity.class));
//                }
//                else

                if (url.contains("10086"))
                {
                    onReceivedError( view, 500,
                            "fuck", url);
                    return true;
                }
                if (!url.contains("kldy")&&(!url.contains("http://192.168.1.200")))
                {
                    onReceivedError( view, 500,
                            "fuck", url);
                    return true;
                }
                if (url.equals(URL_SCRIPT)) {
                    //// : 2016/11/1 0001 剧本
                    startActivityRightToLeft(new Intent(getActivity(), ScriptActivity.class));
                } else if (url.equals(URL_COLUMN)) {
                    //// : 2016/11/1 0001 大咖
                    startActivityRightToLeft(new Intent(getActivity(), NewInfoActivity.class));
                } else if (url.equals(URL_CHIPS)) {
                    //// : 2016/11/1 0001 项目
                    startActivityRightToLeft(new Intent(getActivity(), ProjectActivity.class));
                }
//                else if (url.equals(URL_NEARBY)) {
//                    //// : 2016/11/1 0001 附近
////                    startActivityRightToLeft(new Intent(getActivity(), NearbyActivity.class));
//                    WebViewActivity.startActivity(getActivity(), url);
//
//                }
                else
                {
                    //// TODO: 2016/11/1 0001 跳转倒其它act打开界面
//                    WebViewActivity.startActivity(getActivity(), url);
                    Intent intent = new Intent();
                    String resultUrl = AppWebUrl.createUrl(url);
                    intent.putExtra("url",resultUrl);
                    intent.setClass( getActivity(), MyWebActivity.class);
                    ((Main2Activity)getActivity()).startActivityRightToLeft(intent);
                    view.loadUrl(urlString);
                }

                //停止加载
                view.stopLoading();
                return false;
            }
        });
        webView.loadUrl(urlString);
    }
}
